package com.lhc.dao.impl;

import com.lhc.dao.UserDao;
import com.lhc.pojo.User;
import com.lhc.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.JDBCType;
import java.sql.SQLException;

/**
 * @author lhc
 * @version V1.0
 * @date 2021/06/30
 **/
public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
    /**
     * 添加用户
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(User user) throws SQLException {
        String sql = "INSERT INTO t_user(user_name,PASSWORD,email)\n" +
                "VALUE (?,?,?)";
        return queryRunner.update(JdbcUtils.getConnection(),sql,user.getUserName(),
                user.getPassword(),user.getEmail());
    }

    /**
     * 查找用户
     * @param username
     * @return
     * @throws SQLException
     */
    @Override
    public User findByName(String username) throws SQLException {
        String sql = "SELECT user_id userId,user_name userName,PASSWORD,email\n" +
                "FROM t_user WHERE user_name = ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(User.class),username);
    }
}
